﻿/// <reference path="jquery/jquery-1.9.1.js" />
$(function () {
    //状态筛选
    $('.status li a').click(function () {
        $('.status li a').removeClass('current');
        $(this).addClass('current');
        $.getJSON('/handlers/postajax.ashx', { action: 'load_status_post', status: $(this).attr('data-status'), cat: $('#sel_category').val() }, function (data) {
            renderPostList(data);
            rePager(data);
        });
    });
    
    //批量操作
    $('.do-action').click(function () {
        var action = $(this).prev('.sel-action').val();
        if (action == 'edit') {

        } else if (action == 'trash') {
            var ids = new Array;
            $('.cb-item:checked').each(function () { ids[ids.length] = $(this).attr('id').replace('cb_post_', ''); })
            $.get('/handlers/postajax.ashx', { action: 'move_trash', ids: ids.toString() }, function (res) {
                if (res == 'success') {
                    $.getJSON('/handlers/postajax.ashx', { action: 'load_post', page: 1, size: 30, status: $('.status a[class*="current"]').attr('data-status'), cat: $('#sel_category').val() }, function (data) {
                        renderPostList(data);
                        rePager(data);
                    });
                }
            });
        } else if (action == 'normal') {
            var ids = new Array;
            $('.cb-item:checked').each(function () { ids[ids.length] = $(this).attr('id').replace('cb_post_', ''); })
            $.get('/handlers/postajax.ashx', { action: 'move_normal', ids: ids.toString() }, function (res) {
                if (res == 'success') {
                    $.getJSON('/handlers/postajax.ashx', { action: 'load_post', page: 1, size: 30, status: $('.status a[class*="current"]').attr('data-status'), cat: $('#sel_category').val() }, function (data) {
                        renderPostList(data);
                        rePager(data);
                    });
                }
            });
        }
    });

    //筛选
    $('.do-select').click(function () {
        var id = $('#sel_category').val();
        $.getJSON('/handlers/postajax.ashx', { action: 'load_category_post', status: $('.status a[class*="current"]').attr('data-status'), cat: id }, function (data) {
            renderPostList(data);
            rePager(data);
        });
    });

    $('.trash a').click(function () {
        return false;
    });

    ////左右键换页
    //$(document).keydown(function () {
    //    if (event.keyCode == 37 || event.keyCode == 33) {
    //        var pager = parseInt($('#pager .active a').text())-1;
    //        pager = pager < 1 ? 1 : pager;
    //        setPage(pager);
    //        var options = {
    //            currentPage: pager,
    //            totalPages: parseInt($('#pager').attr('data-tatal')),
    //            numberOfPages: parseInt($('#pager').attr('data-tatal')) > 5 ? 5 : parseInt($('#pager').attr('data-tatal')),
    //            size: 'small',
    //            useBootstrapTooltip: true,
    //            tooltipTitles: function (type, page, current) {
    //                if (page == current) {
    //                    return '当前第' + page + '页';
    //                }
    //                switch (type) {
    //                    case 'first':
    //                        return '返回第一页';
    //                    case 'prev':
    //                        return '上一页';
    //                    case 'next':
    //                        return '下一页';
    //                    case 'last':
    //                        return '跳到最后一页';
    //                    case 'page':
    //                        return '跳到第' + page + '页';
    //                }
    //            },
    //            onPageChanged: function (e, oldPage, newPage) {
    //                setPage(newPage);
    //            }
    //        }
    //        //$('#pager').empty();
    //        $('#pager').bootstrapPaginator(options);
    //    } else if (event.keyCode == 39 || event.keyCode == 34) {
    //        alert(2);
    //    }
    //})
});

//改变页码
function setPage(pageIndex) {
    $.getJSON('/handlers/postajax.ashx', { action: 'load_post', page: pageIndex, size: 30, status: $('.status a[class*="current"]').attr('data-status'), cat: $('#sel_category').val() }, function (data) {
        renderPostList(data);
    });
}

///渲染列表
function renderPostList(data) {
    var $postList = $('#post_list').empty();
    var list = data.List;
    $(list).each(function (i, item) {
        $postList.append('<tr id="post_' + item.Id + '">' +
                        '<td class="text-center">' +
                        '<label for="cb_post_' + item.Id + '" class="hide-text">选择 ' + item.Title + '</label>' +
                        '<input id="cb_post_' + item.Id + '" class="cb-post" type="checkbox" /></td>' +
                        '<td>' +
                        '<strong><a href="#' + item.Id + '">' + item.Title + '</a></strong>' +
                        '<div class="row-actions">' +
                        '<span class="open-edit"><a href="#editor?post=' + item.Id + '" title="编辑此项目">编辑</a> | </span>' +
                        //'<span class="inline-edit"><a href="#" title="实时编辑此项目">快速编辑</a> | </span>' +
                        '<span class="trash"><a title="移动此项目到回收站" href="#trash_' + item.Id + '">移至回收站</a> | </span>' +
                        '<span class="view"><a href="#' + item.Id + '" title="查看“' + item.Title + '”">查看</a></span>' +
                        '</div>' +
                        '</td>' +
                        '<td>' + item.Category + '</td>' +
                        '<td>' + item.Tag + '</td>' +
                        '<td class="text-center"><span class="badge">' + item.CommentCount + '<span></td>' +
                        '<td>' + item.Date + '</td>' +
                        '</tr>');
    });
    if (list.length == 0) {
        $postList.append('<tr id="post_null"><td class="text-center">&nbsp;</td><td><strong><a href="javascript:;">没有记录</a></strong><div class="row-actions">&nbsp;</div></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>');
    }
}
//重新分页
function rePager(data) {
    $('#pager').empty();
    if (data.TotalCount != 0) {
        var options = {
            currentPage: 1,
            totalPages: Math.ceil(data.TotalCount / 30),
            numberOfPages: Math.ceil(data.TotalCount / 30) > 5 ? 5 : Math.ceil(data.TotalCount / 30),
            size: 'small',
            useBootstrapTooltip: true,
            tooltipTitles: function (type, page, current) {
                if (page == current) {
                    return '当前第' + page + '页';
                }
                switch (type) {
                    case 'first':
                        return '返回第一页';
                    case 'prev':
                        return '上一页';
                    case 'next':
                        return '下一页';
                    case 'last':
                        return '跳到最后一页';
                    case 'page':
                        return '跳到第' + page + '页';
                }
            },
            onPageChanged: function (e, oldPage, newPage) {
                setPage(newPage);
            }
        }
        $('#pager').bootstrapPaginator(options);
    }
}